import store from "../store/index.js"
import "./platform.js"



export default function myRequest(api, body, method = 'POST', mode = 'form') {
	let platform = this.platformUtil.getPlatForm();
	let contentType = "";
	if (mode == "json") {
		contentType = "application/json";
		body = JSON.stringify(body)
	} else {
		contentType = "application/x-www-form-urlencoded";
	}
	console.log("请求接口"+store.state.baseUrl+api)
	uni.showLoading({
		title:"加载中..."
	})
	let header = {
		token: store.state.token,
		lat: store.state.lat,
		lng: store.state.lng,
		ip: store.state.ip,
		"Content-Type": contentType,
		platform:platform,
		platform1:store.state.sysInfo.platform
	}
	console.log(header)
	return new Promise(function(res, rej) {
		uni.request({
			url: store.state.baseUrl + api,
			method:method,
			header:header,
			data: body,
			success:data=> {
				console.log(data,111111111)
				// //console.log("请求接口=="+JSON.stringify(data))
				if (data.data.code == 0) {
					res(data.data);
					if(data.data.msg&&data.data.msg!='操作成功'){
						uni.showToast({
							title:data.data.msg,
							icon:'none'
						})
					}
				}if (data.data.code == 999) {
					console.log("登陆失效")
					store.commit("setToken","")
					// uni.showModal({
					// 	title:'提示',
					// 	content:'登录失效',
					// 	showCancel:false,
					// 	success(res) {
					// 		if(res.confirm){
					// 			// 重新跳转首页
					// 			uni.reLaunch({
					// 				url:"/pages/login/login",
					// 				success: (res) => {
					// 				},
					// 				fail: (err) => {
					// 					console.log("error=>"+JSON.stringify(err))
					// 				}
					// 			})
					// 		}
					// 	}
					// })
					
				}if(data.data.code == 500){
					res(data.data);
					uni.showToast({
						title:data.data.msg,
						icon:'none'
					})
				}if (data.data.code == 401) {
					uni.showToast({
						title:data.data.msg,
						icon:'none'
					})
				}else {
					rej(data);
				}
				uni.hideLoading()
			},
			fail:err=> {
				uni.hideLoading()
				console.log("请求失败=="+JSON.stringify(err))
			}
		})
	})

}
